package com.zhanggm.zyk.hetong.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.itext.pdf.service.PdfService;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.bw.zhanggm.file.config.AjaxResult;
import com.sun.media.sound.FFT;
import com.zhanggm.zyk.hetong.entity.Hetong;
import com.zhanggm.zyk.hetong.feign.FujianOpenFeignService;
import com.zhanggm.zyk.hetong.service.IHetongService;
import com.zhanggm.zyk.hetong.vo.Fujian;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-03-12
 */
@Slf4j
@RestController
@RequestMapping("/hetong")
public class HetongController {

    @Autowired
    private IHetongService iHetongService;

    @Autowired
    private PdfService pdfService;

    @Autowired
    FileService fileService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    FujianOpenFeignService fujianOpenFeignService;


    /**
     * 合同列表分页查询接口
     * @param hetong
     * @param page
     * @return
     */
    @GetMapping("page")
    public AjaxResult page(Hetong hetong, Page<Hetong> page) {

        //查询条件
        LambdaQueryWrapper<Hetong> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //时间区间查询
        lambdaQueryWrapper.ge(hetong.getShiJianStart() != null, Hetong::getShiJian, hetong.getShiJianStart());
        if (hetong.getShiJianEnd() != null) {
            lambdaQueryWrapper.le(Hetong::getShiJian, hetong.getShiJianEnd());
        }
        //贷款方查询
        lambdaQueryWrapper.like(hetong.getDaiMingch() != null, Hetong::getDaiMingch, hetong.getDaiMingch());

        //查询
        Page<Hetong> pageInfo = iHetongService.page(page, lambdaQueryWrapper);

        return AjaxResult.success(pageInfo);
    }

    /**
     * 合同保存更新接口
     * @param hetong
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody Hetong hetong) {
        //校验表单
        if (StrUtil.isBlank(hetong.getDaiMingch()) || StrUtil.isBlank(hetong.getJieMingch())) {
            return AjaxResult.error("表单信息不能为空，请注意");
        }
        //保存合同
        iHetongService.saveOrUpdate(hetong);
        //发送kafka消息
        kafkaTemplate.send("hetong0312", UUID.randomUUID() + "", JSONUtil.toJsonStr(hetong));
        return AjaxResult.success();
    }


    /**
     * 合同导出接口
     * @param response
     * @throws IOException
     */
    @GetMapping("pdfDown")
    public void pdfDown(HttpServletResponse response) throws IOException {
        List<Hetong> list = iHetongService.list();
        String fileName = UUID.randomUUID()+".pdf";
        pdfService.export(list, fileName, "合同列表", Hetong.class);
        pdfService.sign(fileName,"sign"+ fileName);
        pdfService.download("sign" + fileName,response);
    }


    /**
     * 合同详情
     * @param id
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(String id) {
        Hetong hetong = iHetongService.getById(id);
        Fujian fujian = fujianOpenFeignService.getById(hetong.getFujianId());
        hetong.setDaiMingch(fujian.getFileName());
        return AjaxResult.success(hetong);
    }





}
